using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;

namespace EuropeEngulfed.NET
{
    public partial class StartGameDialog : Form
    {
        public enum StartOptions { hotSeat, networkHostAsAxis, networkHostAsAllies, networkConnect };

        private IPAddress IPAddressToConnect;
        private bool fromLoadGame = false;

        public StartGameDialog()
        {
            InitializeComponent();
            hostAsAxisRadioButton.Checked = true;
            hotSeatRadioButton.Checked = true;
            IPAddressTextBox.Text = "192.168.1.2";
        }

        public StartGameDialog(bool fromLoadGame) : this()
        {
            this.fromLoadGame = fromLoadGame;
            this.connectRadioButton.Enabled = false;
        }

        public StartOptions StartOption
        {
            get
            {
                if (hotSeatRadioButton.Checked)
                    return StartOptions.hotSeat;
                else if (connectRadioButton.Checked)
                    return StartOptions.networkConnect;
                else if (hostAsAlliesRadioButton.Checked)
                    return StartOptions.networkHostAsAllies;
                else
                    return StartOptions.networkHostAsAxis;
            }
        }

        public IPAddress IPAddressToConnectTo
        {
            get
            {
                return IPAddressToConnect;
            }
        }

        private void hotSeatRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            TCPIPGroupBox.Enabled = !hotSeatRadioButton.Checked;
            IPAddressTextBox.Enabled = connectRadioButton.Checked;
        }

        private void connectRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            IPAddressTextBox.Enabled = connectRadioButton.Checked;
        }

        private void continueButton_Click(object sender, EventArgs e)
        {
            if(StartOption == StartOptions.networkConnect)
            {
                try
                {
                    IPAddressToConnect = IPAddress.Parse(IPAddressTextBox.Text);
                }
                catch
                {
                    MessageBox.Show("Invalid IP address selected.");
                    return;
                }
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}